package com.dlc.shop.jky.common.foreign.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.dlc.shop.bean.model.Brand;
import com.dlc.shop.bean.model.Category;
import com.dlc.shop.common.foreign.ipPortMap;
import com.dlc.shop.config.ShopConfig;
import com.dlc.shop.jky.common.foreign.service.ForeignProbranCategoryService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
@Slf4j
public class ForeignProbranCategoryServiceImpl implements ForeignProbranCategoryService {

    private final ShopConfig shopConfig;

    public String getUrlIpPort(){
        ipPortMap ipPortMap = shopConfig.getForeignUrl();
        return ipPortMap.getUrl();
    }


    @Override
    public String save(Category category) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
       if (urlIpPort != null && urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/category/save";
        return post(url,category);
    }

    @Override
    public String update(Category category) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/category/update";
        return post(url,category);
    }

    @Override
    public String delete(Category category) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/category/delete";
        return post(url,category);
    }

    @Override
    public String plaSave(Category category) {
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/category/plaSave";
        return post(url,category);
    }

    @Override
    public String plaUpdate(Category category) {
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/category/plaUpdate";
        return post(url,category);
    }

    @Override
    public String plaDelete(Long categoryId) {
        //return post("http://192.168.3.26:8089/synchronize/category/plaDelete");
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/category/plaDelete/"+categoryId;
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json"); // 设置请求头
                //.body(JSON.toJSONString(categoryId)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+url+" 返回数据---》"+response.body());
        return response.body();
    }

    private String post(String url ,Category category){
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(category)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+url+" 返回数据---》"+response.body());
        return response.body();
    }


    @Override
    public String plaDeleteBrand(Long brandId) {
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/brand/delete/"+brandId;
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json"); // 设置请求头
        //.body(JSON.toJSONString(brandId)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+url+" 返回数据---》"+response.body());
        return response.body();
    }

    @Override
    public String plaUpdateBrand(Brand brand) {
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/brand/update";
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(brand)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+url+" 返回数据---》"+response.body());
        return response.body();
    }

    @Override
    public String plaSaveBrand(Brand brand) {
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/brand/save";
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(brand)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+url+" 返回数据---》"+response.body());
        return response.body();
    }



}
